
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 *
 */

package com.hlkj.pay.app.token.ext.impl;

import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.common.IAuthCodeService;
import com.hlkj.pay.app.token.ext.AuthTokenExtPt;
import com.hlkj.pay.common.AdminUserResultCode;
import com.hlkj.pay.common.TenantSecretContext;
import com.hlkj.pay.common.constants.TenantAuthConstant;
import com.hlkj.pay.enums.AdminUserEnum;
import com.hlkj.pay.enums.AuthTokenEnum;
import com.hlkj.pay.enums.UserEnum;
import com.hlkj.pay.infrastructure.model.token.TokenSecretDO;
import com.hlkj.pay.infrastructure.model.user.UserDO;
import com.hlkj.pay.service.token.impl.TokenCommonService;
import com.hlkj.pay.service.user.IUserService;
import com.hlkj.pay.vo.common.req.token.AuthTokenClientRequest;
import com.hlkj.pay.vo.common.req.token.PhoneTokenRequest;
import com.hlkj.pay.vo.common.resp.token.OauthTokenResp;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/09/30 10:42
 */
@Slf4j
@Service
@Extension(bizId = TenantAuthConstant.CLIENT_PHONE_AUTH_CODE)
@RequiredArgsConstructor
public class ClientPhoneAuthCodeTokenServiceExt implements AuthTokenExtPt {

    private final IUserService userService;

    private final IAuthCodeService authCodeService;

    private final TokenCommonService tokenCommonService;

    /**
     * OauthTokenResp
     * 
     * @return
     */
    @Override
    public CommonResult<OauthTokenResp> oauthToken(AuthTokenClientRequest authTokenClientRequest) {
        PhoneTokenRequest phoneTokenRequest = (PhoneTokenRequest) authTokenClientRequest;
        CommonResult<Void> voidCommonResult = authCodeService.checkAuthCode(phoneTokenRequest.getPhone(), AdminUserEnum.AdminAuthCodeEnum.AUTH_CODE_LOGIN,
                phoneTokenRequest.getSmsCode());
        if (!voidCommonResult.isSuccess()) {
            return CommonResult.error(voidCommonResult.getCode(), voidCommonResult.getMsg());
        }
        TokenSecretDO tenantSecretDO = TenantSecretContext.get();
        UserDO user = null;
        try {
            user = userService.queryUserByPhone(phoneTokenRequest.getPhone());
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (user == null) {
            return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
        }
        if (!UserEnum.USER_STATUS.ABLE.getCode().equals(user.getStatus())) {
            return CommonResult.error(AdminUserResultCode.TOKEN_USER_INFO_NOT_EFFECT);
        }
        OauthTokenResp oauthTokenResp;
        AuthTokenEnum.AUTH_TOKEN_REQUEST_TYPE authTokenClientType = AuthTokenEnum.AUTH_TOKEN_REQUEST_TYPE.PHONE;
        // 普通认证
        oauthTokenResp = tokenCommonService.userToken(tenantSecretDO, user.getId(), null);
        tokenCommonService.addTenantAuthLog(tenantSecretDO, oauthTokenResp, authTokenClientType);
        return CommonResult.success(oauthTokenResp);
    }

}
